package c.c.b.a.a;

import com.pgl.sys.ces.out.ISdkLite;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Objects;

/* compiled from: RealBufferedSource */
public final class r implements g {
    public final e a = new e();
    public final w b;
    public boolean c;

    /* compiled from: RealBufferedSource */
    public class a extends InputStream {
        public int available() throws IOException {
            r rVar = r.this;
            if (!rVar.c) {
                return (int) Math.min(rVar.a.b, 2147483647L);
            }
            throw new IOException("closed");
        }

        public void close() throws IOException {
            r.this.close();
        }

        public int read() throws IOException {
            r rVar = r.this;
            if (rVar.c) {
                throw new IOException("closed");
            }
            e eVar = rVar.a;
            if (eVar.b == 0 && rVar.b.b(eVar, 8192) == -1) {
                return -1;
            }
            return r.this.a.h() & ISdkLite.REGION_UNSET;
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(r.this);
            stringBuilder.append(".inputStream()");
            return stringBuilder.toString();
        }

        public int read(byte[] bArr, int i, int i2) throws IOException {
            if (r.this.c) {
                throw new IOException("closed");
            }
            y.a((long) bArr.length, (long) i, (long) i2);
            r rVar = r.this;
            e eVar = rVar.a;
            if (eVar.b == 0 && rVar.b.b(eVar, 8192) == -1) {
                return -1;
            }
            return r.this.a.a(bArr, i, i2);
        }
    }

    public r(w wVar) {
        Objects.requireNonNull(wVar, "source == null");
        this.b = wVar;
    }

    public void a(long j) throws IOException {
        if (!n(j)) {
            throw new EOFException();
        }
    }

    public long b(e eVar, long j) throws IOException {
        if (eVar == null) {
            throw new IllegalArgumentException("sink == null");
        } else if (j < 0) {
            throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
        } else if (this.c) {
            throw new IllegalStateException("closed");
        } else {
            e eVar2 = this.a;
            if (eVar2.b == 0 && this.b.b(eVar2, 8192) == -1) {
                return -1;
            }
            return this.a.b(eVar, Math.min(j, this.a.b));
        }
    }

    public e c() {
        return this.a;
    }

    public void close() throws IOException {
        if (!this.c) {
            this.c = true;
            this.b.close();
            this.a.B0();
        }
    }

    public boolean e() throws IOException {
        if (!this.c) {
            return this.a.e() && this.b.b(this.a, 8192) == -1;
        } else {
            throw new IllegalStateException("closed");
        }
    }

    public InputStream f() {
        return new a();
    }

    public long f0(byte b) throws IOException {
        return a(b, 0, Long.MAX_VALUE);
    }

    public byte h() throws IOException {
        a(1);
        return this.a.h();
    }

    public short i() throws IOException {
        a(2);
        return this.a.i();
    }

    public boolean isOpen() {
        return this.c ^ 1;
    }

    public int j() throws IOException {
        a(4);
        return this.a.j();
    }

    public void k(byte[] bArr) throws IOException {
        try {
            a((long) bArr.length);
            this.a.v(bArr);
        } catch (EOFException e) {
            int i = 0;
            while (true) {
                e eVar = this.a;
                long j = eVar.b;
                if (j > 0) {
                    int a = eVar.a(bArr, i, (int) j);
                    if (a != -1) {
                        i += a;
                    } else {
                        throw new AssertionError();
                    }
                }
                throw e;
            }
        }
    }

    public byte[] l(long j) throws IOException {
        if (n(j)) {
            return this.a.l(j);
        }
        throw new EOFException();
    }

    public String m(Charset charset) throws IOException {
        if (charset != null) {
            this.a.n(this.b);
            e eVar = this.a;
            Objects.requireNonNull(eVar);
            try {
                return eVar.u(eVar.b, charset);
            } catch (EOFException e) {
                throw new AssertionError(e);
            }
        }
        throw new IllegalArgumentException("charset == null");
    }

    public boolean n(long j) throws IOException {
        if (j < 0) {
            throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
        } else if (this.c) {
            throw new IllegalStateException("closed");
        } else {
            e eVar;
            do {
                eVar = this.a;
                if (eVar.b >= j) {
                    return true;
                }
            } while (this.b.b(eVar, 8192) != -1);
            return false;
        }
    }

    public String p() throws IOException {
        return z(Long.MAX_VALUE);
    }

    public int read(ByteBuffer byteBuffer) throws IOException {
        e eVar = this.a;
        if (eVar.b == 0 && this.b.b(eVar, 8192) == -1) {
            return -1;
        }
        return this.a.read(byteBuffer);
    }

    public String toString() {
        StringBuilder g = c.b.a.a.a.g("buffer(");
        g.append(this.b);
        g.append(")");
        return g.toString();
    }

    public boolean x(long j, h hVar) throws IOException {
        int v = hVar.v();
        if (this.c) {
            throw new IllegalStateException("closed");
        } else if (j < 0 || v < 0 || hVar.v() - 0 < v) {
            return false;
        } else {
            int i = 0;
            while (i < v) {
                long j2 = ((long) i) + j;
                if (!n(1 + j2) || this.a.w(j2) != hVar.a(0 + i)) {
                    return false;
                }
                i++;
            }
            return true;
        }
    }

    public String z(long j) throws IOException {
        if (j >= 0) {
            long j2 = j == Long.MAX_VALUE ? Long.MAX_VALUE : j + 1;
            long a = a((byte) 10, 0, j2);
            if (a != -1) {
                return this.a.x0(a);
            }
            if (j2 < Long.MAX_VALUE && n(j2) && this.a.w(j2 - 1) == (byte) 13 && n(1 + j2) && this.a.w(j2) == (byte) 10) {
                return this.a.x0(j2);
            }
            e eVar = new e();
            e eVar2 = this.a;
            eVar2.r(eVar, 0, Math.min(32, eVar2.b));
            StringBuilder g = c.b.a.a.a.g("\\n not found: limit=");
            g.append(Math.min(this.a.b, j));
            g.append(" content=");
            g.append(eVar.z0().t());
            g.append(8230);
            throw new EOFException(g.toString());
        }
        throw new IllegalArgumentException(c.b.a.a.a.B("limit < 0: ", j));
    }

    public long a(byte b, long j, long j2) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        } else if (j < 0 || j2 < j) {
            throw new IllegalArgumentException(String.format("fromIndex=%s toIndex=%s", new Object[]{Long.valueOf(j), Long.valueOf(j2)}));
        } else {
            while (j < j2) {
                long k = this.a.k(b, j, j2);
                if (k == -1) {
                    e eVar = this.a;
                    long j3 = eVar.b;
                    if (j3 >= j2 || this.b.b(eVar, 8192) == -1) {
                        break;
                    }
                    j = Math.max(j, j3);
                } else {
                    return k;
                }
            }
            return -1;
        }
    }

    public h c(long j) throws IOException {
        if (n(j)) {
            return this.a.c(j);
        }
        throw new EOFException();
    }

    public void p(long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        while (j > 0) {
            e eVar = this.a;
            if (eVar.b == 0 && this.b.b(eVar, 8192) == -1) {
                throw new EOFException();
            }
            long min = Math.min(j, this.a.b);
            this.a.p(min);
            j -= min;
        }
    }

    public int l() throws IOException {
        a(4);
        return this.a.l();
    }

    public short k() throws IOException {
        a(2);
        return this.a.k();
    }

    public long m() throws IOException {
        a(1);
        int i = 0;
        while (true) {
            int i2 = i + 1;
            if (!n((long) i2)) {
                break;
            }
            byte w = this.a.w((long) i);
            if ((w >= (byte) 48 && w <= (byte) 57) || ((w >= (byte) 97 && w <= (byte) 102) || (w >= (byte) 65 && w <= (byte) 70))) {
                i = i2;
            } else if (i == 0) {
                throw new NumberFormatException(String.format("Expected leading [0-9a-fA-F] character but was %#x", new Object[]{Byte.valueOf(w)}));
            }
        }
        return this.a.m();
    }

    public x a() {
        return this.b.a();
    }
}
